package com.yanqu.road.dao.impl.monopolymarket.resgrab;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.resgrab.MonopolyMarketResGrabFinishData;
import com.yanqu.road.utils.date.DateHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/9/5 14:32
 */
public class MonopolyMarketResGrabFinishDataDaoImpl extends TempDataObjectDao<MonopolyMarketResGrabFinishData> {
    public static String getMonopolyMarketResGrabFinishDataSql = "select * from `t_c_monopolymarket_resgrab_finish` where (`isReceive`=0 or `isJoinRank`=0 )and `time` > ?";
    public List<MonopolyMarketResGrabFinishData> getMonopolyMarketResGrabFinishDataList() {
        ResultSet rs = null;
        CallableStatement call = null;
        int time = (int)(DateHelper.getCurrentSecond() - 30 * DateHelper.DAY_SECONDS);
        List<MonopolyMarketResGrabFinishData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getMonopolyMarketResGrabFinishDataSql);
            int i = 1;
            call.setInt(i++, time);
            rs = call.executeQuery();
            while (rs.next()) {
                MonopolyMarketResGrabFinishData item = parse(rs);
                list.add(item);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateMonopolyMarketResGrabFinishDataSql = "update `t_c_monopolymarket_resgrab_finish` set type=?,userId=?,posIndex=?,side=?,level=?,robName=?,objectStr=?,buffValues=?,rewardTime=?,rewardNum=?,totalRewardNum=?,startTime=?,totalTIme=?,isReceive=?,isJoinRank=?,time=? where id=?";
    @Override
    protected boolean updateData(MonopolyMarketResGrabFinishData monopolyMarketResGrabFinishData) {
        monopolyMarketResGrabFinishData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.TINYINT, monopolyMarketResGrabFinishData.getType()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketResGrabFinishData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getPosIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getSide()));
        para.put(i++, new DbParameter(Types.TINYINT, monopolyMarketResGrabFinishData.getLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketResGrabFinishData.getRobName()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketResGrabFinishData.getObjectStr()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getBuffValues()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getRewardTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getRewardNum()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getTotalRewardNum()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getStartTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getTotalTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, monopolyMarketResGrabFinishData.getIsReceive()));
        para.put(i++, new DbParameter(Types.BOOLEAN, monopolyMarketResGrabFinishData.getIsJoinRank()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getTime()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketResGrabFinishData.getId()));
        if (getDb().executeNoneQuery(updateMonopolyMarketResGrabFinishDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMonopolyMarketResGrabFinishDataSql = "insert into `t_c_monopolymarket_resgrab_finish`(id,type,userId,posIndex,side,level,robName,objectStr,buffValues,rewardTime,rewardNum,totalRewardNum,startTime,totalTime,isReceive,isJoinRank,time) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(MonopolyMarketResGrabFinishData monopolyMarketResGrabFinishData) {
        monopolyMarketResGrabFinishData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketResGrabFinishData.getId()));
        para.put(i++, new DbParameter(Types.TINYINT, monopolyMarketResGrabFinishData.getType()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketResGrabFinishData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getPosIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getSide()));
        para.put(i++, new DbParameter(Types.TINYINT, monopolyMarketResGrabFinishData.getLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketResGrabFinishData.getRobName()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketResGrabFinishData.getObjectStr()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getBuffValues()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getRewardTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getRewardNum()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getTotalRewardNum()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getStartTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getTotalTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, monopolyMarketResGrabFinishData.getIsReceive()));
        para.put(i++, new DbParameter(Types.BOOLEAN, monopolyMarketResGrabFinishData.getIsJoinRank()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabFinishData.getTime()));
        if (getDb().executeNoneQuery(insertMonopolyMarketResGrabFinishDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MonopolyMarketResGrabFinishData parseData(ResultSet rs) throws SQLException {
        MonopolyMarketResGrabFinishData monopolyMarketResGrabFinishData = new MonopolyMarketResGrabFinishData();
        monopolyMarketResGrabFinishData.setId(rs.getLong("id"));
        monopolyMarketResGrabFinishData.setType(rs.getByte("type"));
        monopolyMarketResGrabFinishData.setUserId(rs.getLong("userId"));
        monopolyMarketResGrabFinishData.setPosIndex(rs.getInt("posIndex"));
        monopolyMarketResGrabFinishData.setSide(rs.getInt("side"));
        monopolyMarketResGrabFinishData.setLevel(rs.getByte("level"));
        monopolyMarketResGrabFinishData.setRobName(rs.getString("robName"));
        monopolyMarketResGrabFinishData.setObjectStr(rs.getString("objectStr"));
        monopolyMarketResGrabFinishData.setBuffValues(rs.getInt("buffValues"));
        monopolyMarketResGrabFinishData.setRewardTime(rs.getInt("rewardTime"));
        monopolyMarketResGrabFinishData.setRewardNum(rs.getInt("rewardNum"));
        monopolyMarketResGrabFinishData.setTotalRewardNum(rs.getInt("totalRewardNum"));
        monopolyMarketResGrabFinishData.setStartTime(rs.getInt("startTime"));
        monopolyMarketResGrabFinishData.setTotalTime(rs.getInt("totalTime"));
        monopolyMarketResGrabFinishData.setIsReceive(rs.getBoolean("isReceive"));
        monopolyMarketResGrabFinishData.setIsJoinRank(rs.getBoolean("isJoinRank"));
        monopolyMarketResGrabFinishData.setTime(rs.getInt("time"));

        return monopolyMarketResGrabFinishData;
    }

    public long getMaxRecordId(){
        String sql = "select ifnull(max(id),0) as maxId from t_c_monopolymarket_resgrab_finish";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }



}
